package GUI;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;

import java.awt.GridBagLayout;

import javax.swing.JTextPane;

import java.awt.GridBagConstraints;

import javax.swing.JTable;

import java.awt.Insets;

import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JButton;

import net.miginfocom.swing.MigLayout;

import javax.swing.border.TitledBorder;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.RowFilter;

import ControlLayer.*;
import ModelLayer.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class SalesPanel extends JPanel {
	private JTextField textField;
	private JTable salesTable;
	
	// for table
	private ArrayList<Sale> salesList;
	private SalesHistoryTableModel salesTableModel;
	
	private JLabel finishedSaleCustomer;
	private JLabel finishedSaleEmployee;
	private JTextArea finishedSalePartSales;
	private JLabel finishedSaleDate;
	private JLabel finishedSaleInvoiceNumber;

	/**
	 * Create the panel.
	 */
	public SalesPanel() {
		setLayout(new MigLayout("", "[grow][grow]", "[grow]"));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Salgshistorik", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
		add(panel_1, "cell 0 0,grow");
		panel_1.setLayout(new MigLayout("", "[grow]", "[grow]"));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_1.add(panel_2, "cell 0 0,grow");
		panel_2.setLayout(new MigLayout("", "[grow][]", "[][][grow][]"));
		
		JLabel lblNewLabel = new JLabel("Find et tidligere salg");
		panel_2.add(lblNewLabel, "cell 0 0");
		
		textField = new JTextField();
		textField.setToolTipText("Indtast invoice nummer ");
		panel_2.add(textField, "cell 0 1,growx");
		textField.setColumns(10);
		
		//---------------------TABLE CONTENT-------------------------------------------------------------
		// setting up the table information

		String[] columnNames = {"Kunde", "Ansat", "Total prisen", "Dato", "Salgs Invoice nummer"};
				
		salesList = SaleContainer.getInstance().getsalesArrayList();
		// newsale 
		// add newsale
				
		try {
			salesTableModel = new SalesHistoryTableModel(columnNames, salesList);
		} 
		catch(NullPointerException e) {
			e.printStackTrace();
		}
							
		//final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
		//setting up the actual table
		salesTable = new JTable(salesTableModel);			
		final TableRowSorter<TableModel> saleSorter = new TableRowSorter<TableModel>(salesTableModel);
		salesTable.setRowSorter(saleSorter);
		salesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		salesTable.setFillsViewportHeight(true);
				
		salesTable.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
            		//StringBuilder sb = new StringBuilder();
            	    int selectedRow = salesTable.getSelectedRow();
            	    
            	    String tempPartSales = null;
            	    
            	    if (selectedRow != -1)
            	    {
            	    	try {
            	    		Sale finishedSale = salesList.get(salesTable.convertRowIndexToModel(selectedRow));
            	    	
            	    		
            	    		if(finishedSale.getCustomer().getName() != null) {            	    		
            	    			finishedSaleCustomer.setText("Kunde: " + finishedSale.getCustomer().getName());
            	    		}
            	    		else {
            	    			finishedSaleCustomer.setText("LinieSalg");
            	    		}
            	    		
            	    		
            	    		
            	    		finishedSaleEmployee.setText("Ansat: " + finishedSale.getEmployee().getName());
            	    		finishedSaleDate.setText("Dato: " + finishedSale.getDate());
            	    		finishedSaleInvoiceNumber.setText("Invoice nummer: " + String.valueOf(finishedSale.getSaleInvoiceNumber()));
            	    		
            	    		ArrayList<PartSale> salePartSales = finishedSale.getPartSaleArrayList();
            	    		
            	    		
            	    		for(PartSale ps : salePartSales) {
            	    			finishedSalePartSales.append((String.valueOf(ps.getAmount()) + " stk. " + ps.getItem().getName() + "\n").toString());
            	    		}
            	    		
            	    		
            	    	}
            	    	catch (ArrayIndexOutOfBoundsException aioobe) {
            	    		 aioobe.printStackTrace();
            	    	}
            	    }
            		
            }
           
        });
		
				
		//---------------------END OF TABLE CONTENT-----------------------------------------------------
		
		salesTable.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		
		
		panel_2.add(new JScrollPane(salesTable), "cell 0 2 2 1,grow");
		
		JButton btnNewButton_1 = new JButton("Slet");
		panel_2.add(btnNewButton_1, "cell 1 3");
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Salgs information", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
		add(panel, "cell 1 0,grow");
		panel.setLayout(new MigLayout("", "[grow]", "[grow]"));
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel.add(panel_3, "cell 0 0,grow");
		panel_3.setLayout(new MigLayout("", "[grow][]", "[][25px:n:25px][][grow][][]"));
		
		JLabel lblNewLabel_1 = new JLabel("Salgs info");
		panel_3.add(lblNewLabel_1, "cell 0 0");
		
		finishedSaleCustomer = new JLabel("Kunde: ");
		panel_3.add(finishedSaleCustomer, "cell 0 1");
		
		finishedSaleEmployee = new JLabel("Ansat: ");
		panel_3.add(finishedSaleEmployee, "cell 0 2");
		
		finishedSalePartSales = new JTextArea();
		finishedSalePartSales.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_3.add(new JScrollPane(finishedSalePartSales), "cell 0 3,grow");
		
		finishedSaleDate = new JLabel("Dato: ");
		panel_3.add(finishedSaleDate, "cell 0 4");
		
		finishedSaleInvoiceNumber = new JLabel("Invoice Nummer: ");
		panel_3.add(finishedSaleInvoiceNumber, "cell 0 5");
		
		JButton btnNewButton = new JButton("S\u00F8g");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String saleText = textField.getText();
					if(saleText.length() == 0) {
						saleSorter.setRowFilter(null);
					} else {
						saleSorter.setRowFilter(RowFilter.regexFilter(saleText));
					}
			}
		});
		panel_2.add(btnNewButton, "cell 1 1");
		
	}
	
	
	
}
